package top.fzqblog.firstapp;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.Toast;

import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 抽离 on 2017/10/5 0005.
 */

public class FloatingBtnTest extends AppCompatActivity {

    @BindView(R.id.usernameWrapper)
    public TextInputLayout userNameWrapper;

    @BindView(R.id.passwordWrapper)
    public TextInputLayout passwordWrapper;

    @BindView(R.id.login)
    public Button login;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.floating_btn);
        ButterKnife.bind(this);
        initBinding();
    }

    private void initBinding() {
        RxView
                .clicks(login)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(
                        Void ->{
                            hideKeyboard();
                            String userName = userNameWrapper.getEditText().getText().toString();
                            String password = passwordWrapper.getEditText().getText().toString();
                            Toast.makeText(this, "username:" + userName + "\n" + "password:" + password,
                                    Toast.LENGTH_SHORT).show();
                        }
                );
    }

    private void hideKeyboard() {
        View view = getCurrentFocus();
        if (view != null) {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).
                    hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
